#include "RT.h"
#include "ASeq.h"
#include "ICounted.h"
namespace clojure_lang {
RT::RT()
{
}

ISeq *RT::seq(CjObject *obj)
{
    ASeq *aseq = obj->dynamicCast<ASeq>();
    if (aseq == NULL)
        return aseq;

    return NULL;
}

int RT::count(CjObject *obj)
{
    ICounted *cnt = obj->dynamicCast<ICounted>();
    if (cnt != NULL)
        return cnt->count();


    return 0;
}

int RT::countFrom(CjObject *o)
{
    if (o == 0)
        return 0;
    else if (o->dynamicCast<IPersistentCollection>() == NULL)
    {
        ISeq *s = seq(o);
        o = NULL;
        int i = 0;
        for(; s != NULL; s = s->next()) {
           if(o->dynamicCast<ICounted>() != NULL)
                return i + s->count();
           i++;
        }
        return i;
    }

    return 0;

}
}
